package com.collection.provider.service.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.collection.api.entity.sys.SysResource;
import com.collection.api.entity.sys.SysRoleResource;
import com.collection.api.sys.SysResourceService;
import com.collection.provider.biz.SysResourceBiz;
import com.collection.provider.business.dao.sys.SysRoleResourceMapper;

@Service(version ="1.0.0")
public class SysResourceServiceImpl implements SysResourceService {

	@Autowired
	private SysResourceBiz sysResourceBiz;
	
	@Autowired SysRoleResourceMapper  sysRoleResourceMapper;

	@Override
	public int deleteByPrimaryKey(Integer id) {
		int deleteByPrimaryKey = sysResourceBiz.deleteByPrimaryKey(id);
		
		return deleteByPrimaryKey;
	}

	@Override
	public int insert(SysResource sysResource) {
		int insert = sysResourceBiz.insert(sysResource);
		return insert;
	}

	@Override
	public int insertSelective(SysResource record) {
		return sysResourceBiz.insertSelective(record);
	}

	@Override
	public SysResource selectByPrimaryKey(Integer id) {
		return sysResourceBiz.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(SysResource record) {
		return 0;
	}

	@Override
	public int updateByPrimaryKey(SysResource record) {
		return sysResourceBiz.updateByPrimaryKey(record);
	}

	@Override
	public List<Map<String, Object>> findAllResource(Integer offset, Integer limit) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(null != offset && !"".equals(offset)){
			map.put("offset", offset);
		}
		if(null != limit && !"".equals(limit)){
			map.put("limit", limit);
		}
		return sysResourceBiz.findAllResource(map);
		
	}

	@Override
	public int findAllResourceCount() {
		return sysResourceBiz.findAllResourceCount();
	}

	@Override
	public List<SysResource> findResourceByRoleId(int roleId) {
		return null;
	}

	@Override
	public List<SysResource> findAllResourceByParentId(int parentId) {
		return null;
	}

	@Override
	public List<SysResource> findResourceByParams(Map<String, Object> params) {
		return null;
	}

	@Override
	public int delRoleMRessourceByRoleId(int roleId) {
		return 0;
	}
	
	/**
	 * 
	 * @Description :获取资源树，带折叠
	 * @author : zongzp
	 * @date :2018年1月3日下午3:55:04
	 * @version : 1.2.0
	 * @return : List<Map<String,Object>>
	 */
	public List<Map<String, Object>> findAllResource() {
//		boolean checked;
		List<Map<String, Object>> treeList = new ArrayList<Map<String,Object>>();
		List<SysResource> findAllResource = sysResourceBiz.findAllResource();
		if(findAllResource.size()>0&&findAllResource!= null){
			for (SysResource sysResource : findAllResource) {
				Map<String, Object> treeMap = new HashMap<>();
				Integer parentId = sysResource.getParentId();
				treeMap.put("pId",parentId );
				treeMap.put("id", sysResource.getId());
				treeMap.put("name", sysResource.getName());
				treeMap.put("linkUrl", sysResource.getUrl());
				treeMap.put("sort", sysResource.getSort());
				int type = sysResource.getType();
				String typ = "";
				if(type==1){
					typ="菜单";
				}else if(type==2){
					typ="模块";
				}else if(type==3){
					typ="子系统";
				}else if(type==4){
					typ="按钮";
				}
				treeMap.put("type",typ);
				treeMap.put("open",true );
				treeList.add(treeMap);
			}
		}
		
		return treeList;
	}
	
	public List<Map<String, Object>> findAllSelectedResource() {
		boolean checked;
		List<Map<String, Object>> treeList = new ArrayList<Map<String,Object>>();
		List<SysResource> findAllResource = sysResourceBiz.findAllResource();
		if(findAllResource.size()>0&&findAllResource!= null){
			for (SysResource sysResource : findAllResource) {
				Map<String, Object> treeMap = new HashMap<>();
				Integer parentId = sysResource.getParentId();
				treeMap.put("pId",parentId );
				treeMap.put("id", sysResource.getId());
				treeMap.put("name", sysResource.getName());
				treeMap.put("linkUrl", sysResource.getUrl());
				treeMap.put("sort", sysResource.getSort());
				List<SysRoleResource> sysRoleResourceList =  sysRoleResourceMapper.getRoleResourceByResouceId(sysResource.getId());
				if(sysRoleResourceList!=null && sysRoleResourceList.size()>0){
					checked = true;
				}else{
					checked = false;
				}
				treeMap.put("checked", checked);
				int type = sysResource.getType();
				String typ = "";
				if(type==1){
					typ="菜单";
				}else if(type==2){
					typ="模块";
				}else if(type==3){
					typ="子系统";
				}else if(type==4){
					typ="按钮";
				}
				treeMap.put("type",typ);
				treeMap.put("open",true );
				treeList.add(treeMap);
			}
		}
		
		return treeList;
	}
 

	
}
